﻿using Kingdee.BOS;
using Kingdee.BOS.WebApi.FormService;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using RK.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.WebApi.Client;

namespace RK
{
    public static class Api
    {
        /// <summary>
        /// 业务关闭,BPW
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId"></param>
        /// <param name="pkEntryIds"></param>
        /// <returns></returns>
        public static ApiResult YLMRPCloseFromClient(Context ctx, string formId, string pkEntryIds)
        {
            var client = new K3CloudApi();
            client.InitClient("6531e4f57eaed9", "267358_474o49DHQvnVxWTK6YWKVdWK7N0dwOtL", "051d0f36cd374b17b704cdcb85c804e7", "system", 2052, "100", "http://erp.bpw.cn:89/k3cloud/");
            var res = client.ExcuteOperation(formId, "YLMRPClose", pkEntryIds);
            ApiResult result = new ApiResult();
            result = JsonConvert.DeserializeObject<ApiResult>(res);
            return result;
        }
        /// <summary>
        /// 扩展金蝶分配接口
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId"></param>
        /// <param name="pkIds"></param>
        /// <param name="tOrgIds"></param>
        /// <returns></returns>
        public static ApiResult Allocate(Context ctx, string formId, int pkIds, string tOrgIds)
        {
            ApiResult apiResult = new ApiResult();
            apiResult.Result = new Result();
            apiResult.Result.ResponseStatus = new ResponseStatus();
            apiResult.Result.ResponseStatus.Errors = new List<ErrorsItem>();
            apiResult.Result.ResponseStatus.SuccessMessages = new List<string>();
            apiResult.Result.ResponseStatus.SuccessEntitys = new List<SuccessEntitysItem>();
            string allocateStr = Base.GetAllocateJson(pkIds, tOrgIds);
            var res = JsonConvert.SerializeObject(WebApiServiceCall.Allocate(ctx, formId, allocateStr));
            JObject result = JObject.Parse(res);
            var isSuccess = result["Result"]["ResponseStatus"]["IsSuccess"].Value<bool>();
            if (isSuccess)
            {
                var billno = result["Result"]["Number"].Value<string>();
                var pkId = result["Result"]["Id"].Value<int>();
                var sEntitys = result["Result"]["ResponseStatus"]["SuccessEntitys"] as JArray;
                apiResult.Result.Number = billno;
                apiResult.Result.Id = pkId;
                apiResult.Result.ResponseStatus.IsSuccess = isSuccess;
                apiResult.Result.ResponseStatus.SuccessMessages.Add("api分配成功！");
                foreach (var item in sEntitys)
                {
                    var sEntity = new SuccessEntitysItem();
                    sEntity.Number = item["Number"].ToString();
                    sEntity.Id = item["Id"].Value<int>();
                    sEntity.DIndex = item["DIndex"].Value<int>();
                }
            }
            else
            {
                apiResult.Result.ResponseStatus.IsSuccess = isSuccess;
                var errs = result["Result"]["ResponseStatus"]["Errors"] as JArray;
                foreach (var err in errs)
                {
                    var errItem = new ErrorsItem();
                    errItem.Message = err["Message"].ToString();
                    errItem.DIndex = (int)err["DIndex"];
                    errItem.FieldName = (string)err["FieldName"];
                    apiResult.Result.ResponseStatus.Errors.Add(errItem);
                }
            }
            return apiResult;
        }
        /// <summary>
        /// 扩展云星空单据查询API
        /// </summary>
        /// <param name="ctx">上下文</param>
        /// <param name="formId">单据标识</param>
        /// <param name="fieldKeys">要查询的字段信息</param>
        /// <param name="filterString">过滤条件</param>
        /// <param name="orderString">排序信息</param>
        /// <param name="limit">查询结果的最大行数</param>
        /// <returns></returns>
        public static List<List<object>> ExecuteBillQuery(Context ctx, string formId, string fieldKeys, string filterString, string orderString, int limit)
        {
            var res = WebApiServiceCall.ExecuteBillQuery(ctx, Base.QueryParameter(formId, fieldKeys, filterString, orderString, limit));
            return res;
        }
        /// <summary>
        /// 下推接口
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object Push(Context ctx, string formId, string data)
        {
            var res = WebApiServiceCall.Push(ctx, formId, data);
            return res;
        }
        /// <summary>
        /// 扩展金蝶Api的Save方法
        /// </summary>
        /// <returns></returns>
        public static ApiResult Save(Context ctx, string formId, string jsonStr)
        {
            ApiResult apiResult = new ApiResult();
            apiResult.Result = new Result();
            apiResult.Result.ResponseStatus = new ResponseStatus();
            apiResult.Result.ResponseStatus.Errors = new List<ErrorsItem>();
            apiResult.Result.ResponseStatus.SuccessMessages = new List<string>();
            var _result = JsonConvert.SerializeObject(WebApiServiceCall.Save(ctx, formId, jsonStr));
            JObject result = JObject.Parse(_result);
            var isSuccess = result["Result"]["ResponseStatus"]["IsSuccess"].Value<bool>();
            if (isSuccess)
            {
                var billno = result["Result"]["Number"].Value<string>();
                var pkId = result["Result"]["Id"].Value<int>();
                apiResult.Result.Number = billno;
                apiResult.Result.Id = pkId;
                apiResult.Result.ResponseStatus.IsSuccess = isSuccess;
                apiResult.Result.ResponseStatus.SuccessMessages.Add("api保存成功！");
            }
            else
            {
                apiResult.Result.ResponseStatus.IsSuccess = isSuccess;
                var errs = result["Result"]["ResponseStatus"]["Errors"] as JArray;
                foreach (var err in errs)
                {
                    var errItem = new ErrorsItem();
                    errItem.Message = err["Message"].ToString();
                    errItem.DIndex = (int)err["DIndex"];
                    errItem.FieldName = (string)err["FieldName"];
                    apiResult.Result.ResponseStatus.Errors.Add(errItem);
                }
            }
            return apiResult;
        }
    }
}
